package thread_test;

import java.util.Scanner;

public class VolatileDemo {
    // 使用 volatile 关键字进行修饰变量 flag 迫使每次读 flag 变量都要从内存中读
    private static volatile int flag = 0;
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Thread t1 = new Thread(()->{
            while (flag == 0){
                // 循环中什么都不写
            }
            System.out.println("t1 线程结束");
        });
        Thread t2 = new Thread(()->{
            System.out.println("请输入 flag 的值:");
            flag = scanner.nextInt();
        });
        t1.start();
        t2.start();
    }
}
